package org.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import entity.Article;
import org.example.dao.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : xuzhennan
 * @Project : 787k-cloud
 * @Package : org.example.service
 * @email : xuzhennan@58.com
 * @date : 2021年08月29日 上午9:31
 */
@Service
public class ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 分页查询文章
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<Article> findArticlesWithPage(int pageNum, int pageSize) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        // 第1页，每页2条
        Page<Article> page = new Page<>(pageNum, pageSize);
        return articleMapper.selectPage(page, wrapper);
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    public Article getArticleDetail(int id) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return articleMapper.selectOne(wrapper);
    }
}
